﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.Metadata;
using System.Windows.Threading;
using CPPEI.Coolzon.UI.Event;
using System.Windows.Media.Animation;
using CPPEI.Coolzon.Event.NCL;
using CPPEI.Coolzon.Event.UI;
using CPPEI.Coolzon.UI.Data;
using CPPEI.Coolzon.Event.Core;

namespace CPPEI.Coolzon.UI.Views
{
    /// <summary>
    /// FreezeWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AutoPlayView : View
    {
        private Storyboard storyboard;
        private TimeSpan Interval;
        private bool IsComplete;

        public double ExcTime
        {
            get
            {
                return (double)GetValue(ExcTimeProperty);
            }
            set
            {
                SetValue(ExcTimeProperty, value);
            }
        }
        private static DependencyProperty ExcTimeProperty = DependencyProperty.Register("ExcTime", typeof(double), typeof(AutoPlayView), new PropertyMetadata(OnPropertyChangedCallback));

        private static void OnPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AutoPlayView autoPlayView = d as AutoPlayView;
            if (Math.Floor(Convert.ToDouble(e.OldValue)) != Math.Floor(Convert.ToDouble(e.NewValue)))
            {
                autoPlayView.tbTime.Text = Math.Floor(Convert.ToDouble(e.NewValue)).ToString();
            }
        }
        public AutoPlayView()
        {
            InitializeComponent();

            double interval = 5;
            if (UIContext.Current.Solution.Displayflow.Interval != 0.0)
            {
                interval = UIContext.Current.Solution.Displayflow.Interval;
            }
            Interval = TimeSpan.FromSeconds(interval);
            //判断是否是全屏
            if (UIContext.Current.IsFullScreen)
            {
                _fullScreenImage.Content = "还原";
            }
            else
            {
                _fullScreenImage.Content = "全屏";
            }

            Init();
        }
        private void Init()
        {
            CPPEI.Coolzon.UI.Views.RenderView renderView = (CPPEI.Coolzon.UI.Views.RenderView)UIContext.Current.RenderView;
            this.tbTime.Text = Interval.Seconds.ToString();
            Scene scene = renderView.CurrentScene;
            Scene nextScene = SolutionUtil.GetNextDisplayflowNode(UIContext.Current.Solution, scene);
            this.tbSceneName.Text = nextScene.Caption;
            this.tbPlaying.Text = "正在播放：" + scene.Caption;
            BeginStory();
        }
        private void BeginStory()
        {
            IsComplete = false;
            if (storyboard != null)
            {
                storyboard.Begin();
                return;
            }
            storyboard = new Storyboard();
            DoubleAnimation timeAnimation = new DoubleAnimation(Interval.TotalSeconds, 1, new Duration(Interval));
            Storyboard.SetTarget(timeAnimation, this);
            Storyboard.SetTargetProperty(timeAnimation, new PropertyPath("ExcTime"));

            DoubleAnimationUsingKeyFrames daMaskLeft = new DoubleAnimationUsingKeyFrames();
            Storyboard.SetTarget(daMaskLeft, MaskLeft);
            Storyboard.SetTargetProperty(daMaskLeft, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));

            EasingDoubleKeyFrame edMaskLeft1 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0)));
            EasingDoubleKeyFrame edMaskLeft2 = new EasingDoubleKeyFrame(180, KeyTime.FromTimeSpan(new TimeSpan(0, 0, Convert.ToInt32(Interval.TotalSeconds / 2))));

            daMaskLeft.KeyFrames.Add(edMaskLeft1);
            daMaskLeft.KeyFrames.Add(edMaskLeft2);

            DoubleAnimationUsingKeyFrames daMaskRight = new DoubleAnimationUsingKeyFrames();
            Storyboard.SetTarget(daMaskRight, MaskRight);
            Storyboard.SetTargetProperty(daMaskRight, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));

            EasingDoubleKeyFrame edMaskRight1 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0)));
            EasingDoubleKeyFrame edMaskRight2 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(new TimeSpan(0, 0, Convert.ToInt32(Interval.TotalSeconds / 2))));
            EasingDoubleKeyFrame edMaskRight3 = new EasingDoubleKeyFrame(180, KeyTime.FromTimeSpan(Interval));

            daMaskRight.KeyFrames.Add(edMaskRight1);
            daMaskRight.KeyFrames.Add(edMaskRight2);
            daMaskRight.KeyFrames.Add(edMaskRight3);

            storyboard.Children.Add(daMaskLeft);
            storyboard.Children.Add(daMaskRight);
            storyboard.Children.Add(timeAnimation);

            storyboard.Completed += storyboard_Completed;
            storyboard.Begin();
        }

        void storyboard_Completed(object sender, EventArgs e)
        {
            ClockGroup clock = sender as ClockGroup;
            if (clock.CurrentState == ClockState.Filling)
            {
                OpenNextDisplayflowNode();
                IsComplete = true;
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            StopAutoPlay();
            this.Cancel();
        }
        public void StopAutoPlay()
        {
            storyboard.Stop();
            storyboard.Remove();
            storyboard = null;

            Common.EventBus.Instance.Publish(new ConsoleStopAutoPlayEvent());
            UIEventBus.Unregister(this);
            NCLEventBus.Unregister(this);
            CoreEventBus.Unregister(this);
        }

        private void OpenNextDisplayflowNode()
        {
            CPPEI.Coolzon.UI.Views.RenderView renderView = (CPPEI.Coolzon.UI.Views.RenderView)UIContext.Current.RenderView;
            Scene nextScene = SolutionUtil.GetNextDisplayflowNode(UIContext.Current.Solution, renderView.CurrentScene);

            NCLEventBus.PublishAsync(new NCLOpenSceneEvent(SwitchMode.Replace, "", nextScene.Name, null));
        }
        

        /// <summary>
        /// 全屏或退出全屏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _fullScreenImage_Click(object sender, RoutedEventArgs e)
        {
            UIEventBus.Publish(new UIChangeFullScreenStateEvent());
            if (UIContext.Current.IsFullScreen)
            {
                _fullScreenImage.Content = "还原";
            }
            else
            {
                _fullScreenImage.Content = "全屏";
            }
        }
    }
}
